package confidence;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

public class ConfidenceService {
    private static String str = "{\n" +
            "\t\"result\": {\n" +
            "\t\t\"unresolvedAddress\": \"\",\n" +
            "\t\t\"res\": [\n" +
            "\t\t\t\"江苏省_1\",\n" +
            "\t\t\t\"南京市_2\",\n" +
            "\t\t\t\"鼓楼区_3A\",\n" +
            "\t\t\t\"湖南路街道_4A\",\n" +
            "\t\t\t\"裴家桥社区居委会_4B\",\n" +
            "\t\t\t\"湖北路_4E-1\",\n" +
            "\t\t\t\"68号_5A-1\",\n" +
            "\t\t\t\"狮子桥_4D\"\n" +
            "\t\t],\n" +
            "\t\t\"townName\": \"湖南路街道\",\n" +
            "\t\t\"coordinate\": \"32.067691,118.777643\",\n" +
            "\t\t\"townCode\": 320106003,\n" +
            "\t\t\"cityCode\": 320100,\n" +
            "\t\t\"provinceCode\": 320000,\n" +
            "\t\t\"fusionRes\": [\n" +
            "\t\t\t\"江苏省_1\",\n" +
            "\t\t\t\"南京市_2\",\n" +
            "\t\t\t\"鼓楼区_3A\",\n" +
            "\t\t\t\"湖南路街道_4A\",\n" +
            "\t\t\t\"裴家桥社区居委会_4B\",\n" +
            "\t\t\t\"湖北路_4E-1\",\n" +
            "\t\t\t\"68号_5A-1\",\n" +
            "\t\t\t\"狮子桥_4D\",\n" +
            "\t\t\t\"5号_5A-1\"\n" +
            "\t\t],\n" +
            "\t\t\"queryRes\": [\n" +
            "\t\t\t\"江苏省_1\",\n" +
            "\t\t\t\"南京市_2\",\n" +
            "\t\t\t\"玄武区_3A\",\n" +
            "\t\t\t\"5号_5A-1\",\n" +
            "\t\t\t\"狮子桥_5B\"\n" +
            "\t\t],\n" +
            "\t\t\"concatRes\": \"江苏省南京市鼓楼区湖南路街道裴家桥社区居委会湖北路68号狮子桥\",\n" +
            "\t\t\"fusionLevel\": \"5A-1\",\n" +
            "\t\t\"oriId\": \"OTqTvZIBqRQIE-aIeX3b\",\n" +
            "\t\t\"countyCode\": 320106,\n" +
            "\t\t\"jdOutputStrId\": \"OTqTvZIBqRQIE-aIeX3b\",\n" +
            "\t\t\"cityName\": \"南京市\",\n" +
            "\t\t\"jdOutputStr\": \"江苏省南京市鼓楼区湖南路街道裴家桥社区居委会湖北路68号狮子桥27号3号楼\",\n" +
            "\t\t\"similarity\": \"5.556071514611904E-6\",\n" +
            "\t\t\"docLevel\": \"4D\",\n" +
            "\t\t\"fusionStr\": \"江苏省南京市鼓楼区湖南路街道裴家桥社区居委会湖北路68号狮子桥5号\",\n" +
            "\t\t\"provinceName\": \"江苏省\",\n" +
            "\t\t\"queryLevel\": \"5B\",\n" +
            "\t\t\"track\": 2,\n" +
            "\t\t\"outString\": \"江苏省南京市鼓楼区湖南路街道裴家桥社区居委会湖北路68号狮子桥27号3号楼\",\n" +
            "\t\t\"countyName\": \"鼓楼区\"\n" +
            "\t},\n" +
            "\t\"message\": \"success\",\n" +
            "\t\"status\": 200\n" +
            "}";

    /**
     * 计算融合置信度级别与潜力级别
     * @return
     */
    private static String confCal() {
        JSONObject jsonObject = JSONObject.parseObject(str);
        JSONObject resultObject = jsonObject.getJSONObject("result");
        //计算是否高潜力
        Confidence.ConfidenceInfo confidence = Confidence.confidence_level(jsonObject,"");

        if ("高".equals(confidence.getLevel())) {
            return confidence.getLevel();
        }


        String original_aoi = Confidence.parseValueFromJsonArray("queryRes", resultObject, "_4D");
        String fusion_aoi = Confidence.parseValueFromJsonArray("fusionRes", resultObject, "_4D");

        String original_poi = Confidence.parseValueFromJsonArray("queryRes", resultObject, "_5B");
        String fusion_poi = Confidence.parseValueFromJsonArray("fusionRes", resultObject, "_5B");

        String original_village = Confidence.parseValueFromJsonArray("queryRes", resultObject, "_4B");
        String fusion_village = Confidence.parseValueFromJsonArray("fusionRes", resultObject, "_4B");

        /**
         * 计算是否高潜力
         */
        HighPotential.Potential potential = HighPotential.highPotential(original_aoi, fusion_aoi, original_poi, fusion_poi, original_village, fusion_village);
        if (! (0 == potential.getType())) {
            return "高";
        }

        return StringUtils.isBlank(confidence.getLevel())? "低" : confidence.getLevel();
    }

    public static void main(String[] args) {
        System.out.println(ConfidenceService.confCal());
    }
}
